package cqrtplm.controller;

import com.alibaba.fastjson.JSONObject;
import cqrtplm.constant.RouteConstant;
import cqrtplm.entity.RtOaFormMappingDO;
import cqrtplm.service.RtOaFormMappingService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.ty.basic.response.ResponseResult;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;

/**
 * OA表单映射控制器
 * 提供OA表单映射相关的REST API接口
 * 
 * @author 张宇
 */
@Slf4j
@RestController
@RequestMapping(RouteConstant.RTOAFORMMAPPING +"v1")
public class RtOaFormMappingController {

    @Resource
    private RtOaFormMappingService rtOaFormMappingService;

    @PostMapping("/insert")
    @ApiOperation("创建OA表单")
    public ResponseResult createRtOaForm(@RequestBody RtOaFormMappingDO entity) {
        log.info("接收到创建OA表单映射记录请求，文档类型：{}，OA表单：{}",
                entity.getDocType(), entity.getOaForm());
        try {
            rtOaFormMappingService.createRtOaFormMapping(entity);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.fail("400",e.getMessage());
        }

        return ResponseResult.success();
    }

    @PutMapping("/update")
    @ApiOperation("更新OA表单")
    public ResponseResult updateRtOaForm(@RequestBody RtOaFormMappingDO entity) {
        try {
            rtOaFormMappingService.updateRtOaFormMapping(entity);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.fail("400",e.getMessage());
        }
        return ResponseResult.success();
    }

    @PostMapping("/delete")
    @ApiOperation("删除OA表单")
    public ResponseResult deleteRtOaForm(@RequestBody String type) {
        try {
            rtOaFormMappingService.deleteRtOaFormMapping(type);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.fail("400",e.getMessage());
        }

        return ResponseResult.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取OA表单")
    public RtOaFormMappingDO getRtOaForm(@PathVariable String  id) {
        return rtOaFormMappingService.getRtOaFormMapping(new BigInteger(id));
    }

    @GetMapping("/list")
    @ApiOperation("获取所有OA表单")
    public ResponseResult listRtOaForm() {
        return ResponseResult.success(rtOaFormMappingService.list());

    }

    @PostMapping("/synchronous")
    public ResponseResult SynchronousOA(@RequestBody JSONObject jsonObject){
        try {
            String url=rtOaFormMappingService.SynchronousOA(jsonObject);
            return ResponseResult.success(url);
        }catch (Exception e){
            return ResponseResult.fail("400",e.getMessage());
        }

    }
}